import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpClient {

    DatagramSocket socket = null;
    String serverIp = null;
    int serverPort;
    public UdpClient(String Ip,int port) throws SocketException {
        socket = new DatagramSocket();
        this.serverIp = Ip;
        this.serverPort=port;
    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要发送的请求");
            String request = sc.next();
            DatagramPacket datagramPacket = new DatagramPacket(request.getBytes(),
                    request.getBytes().length, InetAddress.getByName(serverIp), serverPort);
            //用这个InetAddress这个是把字符串转换为点分十进制
            socket.send(datagramPacket);
            DatagramPacket acc = new DatagramPacket(new byte[4096], 4096);
            socket.receive(acc);
            String outCome = new String(acc.getData(),0, acc.getLength());
            System.out.println(outCome);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpClient udpClient  =new UdpClient("127.0.0.1",9090);
        udpClient.start();
    }
    }

